package com.mytecblog.framework.dao.query;


import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;


import java.util.List;

/**
 * SQL查询
 * @author yg.huang
 * @version v1.0
 *          DATE  2016/10/28
 */
public class SQLDBQuery extends  HQLDBQuery {

    public SQLDBQuery(Class entityClass, String queryStr, Session session) {
        super(entityClass, queryStr, session);
    }
    public SQLDBQuery(String queryStr, Session session) {
        super(null, queryStr, session);
    }
    protected Query createCountQuery(Session session) {
        return session.createNativeQuery("select count(*) from (" + this.queryStr + ") a_");
    }

    protected Query createDataQuery(Session session) {
        String order = this.genOrderByStr();
        NativeQuery query = StringUtils.isNotBlank(order)?session.createNativeQuery(this.queryStr + order,CaseInsensitiveMap.class):session.createNativeQuery(this.queryStr,CaseInsensitiveMap.class);

        return query;
    }
    protected String genOrderByStr() {
        return StringUtils.isBlank(this.orderBy)?(StringUtils.isBlank(this.defaultOrderBy)?"":" order by " + this.defaultOrderBy):" order by " + this.orderBy;
    }



}
